<?php if (!defined('BASEPATH')) exit('No direct access allowed.');

class Kclass {

	//新闻推荐
	public function news($h='',$image='',$listid='',$limit='10')
	{
		
		
		if(!empty($listid)) $lid=array('listid',$listid);else $lid='';
		
		if(empty($image)) $select='kid,ktitle,listid,ktag,ndate,kdescription'; else $select = 'kid,ktitle,listid,kimage,kdescription,ndate';
		$query=$this->new_list('king__article',$h,$image,$lid,$limit,$select,'kid desc');
		
		foreach($query as $ke => $ver){	
			$query[$ke]['url']='/browse/page/'.$ver['listid'].'/'.$ver['kid'];
		}
		return $query;
		
	}


	
	//专题推荐
	public function zhuanti($h='',$image='',$listid='',$limit='10')
	{
		if(!empty($listid)) $lid=array('listid',$listid);else $lid='';
			
		if(empty($image)) $select='kid,ktitle,listid,ktag,ndate,kdescription'; else $select = 'kid,ktitle,listid,kimage,kdescription,ndate';
		$query=$this->new_list('king__zhuanti',$h,$image,$lid,$limit,$select);
		foreach($query as $ke => $ver){	
			$query[$ke]['url']='/browse/page/'.$ver['listid'].'/'.$ver['kid'];
		}
		return $query;
		
	}


	//论坛
	public function bbs($h='',$image='',$userid='',$limit='10')
	{
		$select='threadid,typeid,hit,reply,isbest,username,lastusername,lastdate,style,title'; 
		
		if(!empty($listid)) $lid=array('userid',$userid);else $lid='';
		
		$query=$this->new_list('king_thread',$h,$image,$lid,$limit,$select,'lastdate desc');
		return $query;
		
	}

	//幻灯片推荐
	public function hdp($limit='10')
	{
		$lid="kdescription !='' ";
			
		$select = 'kid,ktitle,kimage,kdescription';
		$query=$this->new_list('king__article','','',$lid,$limit,$select,'kid desc');
		return $query;
		
	}
	
	
	//广告推荐
	public function ad_row($kid='',$kid1='')
	{
	
		if(empty($kid1)){
			$array['kid']=$kid;
		}else{
			$array['kid1']=$kid;
			$array['kname']=$kid1;
		}
		$r = $this->new_row('kname,kcontent','king_block',$array);
		
		echo $r['kcontent'];
	}	
		
	//广告推荐
	public function shangjai_row($limit='10')
	{
		$order='mid desc';
		$query=$this->new_row('*','metas','type = \'tag\'',$order,$limit);
		return $query;		
	}			
		
	/*
		$h nshow,nhot,nhead,ncommend,nup,nfocus//属性
		$listid 栏目名称
		$limit 条数
	*/
	//数据库数据读取//多条
	public function new_list($form='',$h='',$image='',$listid='',$limit='10',$select,$order='')
	{
		$this->_CI = & get_instance();	
		$this->_CI ->db->select($select);
		$this->_CI ->db->from($form);
		
		if(!empty($listid)) $this->_CI ->db->where($listid);	
		$this->_CI->db->where('nshow = 1');
		if(!empty($h)) $this->_CI ->db->where($h,'1');
		
		if(!empty($image)) $this->_CI ->db->where('kimage !=','');
		if(!empty($order)) $this->_CI ->db->order_by($order);	
		$this->_CI ->db->limit($limit);		
		$query = $this->_CI->db->get()->result_array();	

		return $query;
	}
	
	/*
		$h nshow,nhot,nhead,ncommend,nup,nfocus//属性
		$listid 栏目名称
		$limit 条数
	*/
	//数据库单条//
	public function new_row($select,$form='',$where='',$order='',$limit='')
	{
		$this->_CI = & get_instance();	
		$this->_CI ->db->select($select);
		$this->_CI ->db->from($form);	
		$this->_CI->db->where('nshow = 1');
		if(!empty($where)) $this->_CI ->db->where($where);			
		if(!empty($order)) $this->_CI ->db->order_by($order);			
		if($limit > '1') $this->_CI ->db->limit($limit);		
		if(!empty($limit)) {$query = $this->_CI->db->get()->result_array();}else{$query = $this->_CI->db->get()->row_array();}	
		return $query;
	}
		

public function tag_search($keyword)
  {  
  	$keyword = explode(',',$keyword);
  	$this->_CI = & get_instance();	
	$this->_CI -> db->select('ktag,kid,kkeywords,kdescription,kimage');
	$this->_CI->db->where('nshow = 1');		
  	$this->_CI -> db->where_in('ktag',$keyword);
	$this->_CI ->db->from('king_tag');
	$query = $this->_CI->db->get()->result_array();	
	
	return $query;
	
  }



public function m_search($keyword,$num='5',$bs='king__article')
  {  
  	$this->_CI = & get_instance();	

 	//$this->output->cache('30');
 	/*-------模糊查询-------*/
   if($keyword!='')
   {
    $keyword = explode(',',$keyword);//也可以先正则匹配下有没有其他字符作为分隔关
	$dsa=time()-7776000;

	
	$q= implode("%' or kcontent like '%",$keyword);
	 	if(isset($q{0})){
		$q="nshow = 1 and nlastdate >".$dsa." and (kcontent like '%".$q."%')";
	}

	
	$this->_CI->db->where($q);	
    $i = count($keyword);
   // $this->db->like('title',$diqu);



	

    $this->_CI->db->limit($num);// 按分页取出数据
    $this->_CI->db->order_by('kid','desc');
	$this->_CI->db->select('ktitle,kcontent,kid,kimage,listid,nshow');
    $query = $this->_CI->db->get($bs);
    if($query->num_rows() != 0)
    {
     $data = $query->result_array();

    }

   }else{
     $data = '';

   }
  
  foreach($data as $ke => $rs){	
  					
  				if($oneimage=preg_match('/(<img([^>]*))( src=)(["\'])(.*?)\4(([^>]*)\/?>)/is',$rs['kcontent'],$oneimage_array[$ke])){
					$data[$ke]['kimage']=$oneimage_array[$ke][5];
					
					}
	}			
					
					
  return $data;


}










public function sj_search($keyword,$num='5',$bs='posts')
  {  
  	$this->_CI = & get_instance();	

 	//$this->output->cache('30');
 	/*-------模糊查询-------*/
   if($keyword!='')
   {
    $keyword = explode(',',$keyword);//也可以先正则匹配下有没有其他字符作为分隔关
	$dsa=time()-7776000;
	$q= implode("%' or text like '%",$keyword);
	 	if(isset($q{0})){
		$q="nshow = 1 and modified >".$dsa." and (text like '%".$q."%')";
	}
	$this->_CI->db->where($q);
    $this->_CI->db->limit($num);// 按分页取出数据
    $this->_CI->db->order_by('pid','desc');
	$this->_CI->db->select('pid,title,text,kpath');
    $query = $this->_CI->db->get($bs);
    if($query->num_rows() != 0)
    {
     $data = $query->result_array();

    }

   }else{
     $data = '';

   }


  foreach($data as $ke => $rs){	
  					
  				if($oneimage=preg_match('/(<img([^>]*))( src=)(["\'])(.*?)\4(([^>]*)\/?>)/is',$rs['text'],$oneimage_array[$ke])){
					$data[$ke]['kimage']=$oneimage_array[$ke][5];
					
					}
	}			
					
					
  return $data;


}














public function bbs_search($keyword,$num='5',$bs='king_thread')
  {  
  	$this->_CI = & get_instance();	

 	//$this->output->cache('30');
 	/*-------模糊查询-------*/
   if($keyword!='')
   {
    $keyword = explode(',',$keyword);//也可以先正则匹配下有没有其他字符作为分隔关
	$dsa=time()-7776000;
	$q= implode("%' or title like '%",$keyword);
	 	if(isset($q{0})){
		$q="nshow = 1 and lastdate >".$dsa." and (title like '%".$q."%')";
	}
	$this->_CI->db->where($q);
    $this->_CI->db->limit($num);// 按分页取出数据
    $this->_CI->db->order_by('lastdate','desc');
	$this->_CI->db->select('threadid,username,title');
    $query = $this->_CI->db->get($bs);
    if($query->num_rows() != 0)
    {
     $data = $query->result_array();

    }

   }else{
     $data = '';

   }

		
					
  return $data;


}








public function key_search($keyword,$num='5',$bs='king_key')
  {  
  	$this->_CI = & get_instance();	

 	//$this->output->cache('30');
 	/*-------模糊查询-------*/
   if($keyword!='')
   {
    $keyword = explode(',',$keyword);//也可以先正则匹配下有没有其他字符作为分隔关


	$q= implode("%' or ktitle like '%",$keyword);
	 	if(isset($q{0})){
		$q="nshow = 1 and (ktitle like '%".$q."%')";
	}
	$this->_CI->db->where($q);



	$this->_CI->db->where('nshow = 1');
    $this->_CI->db->limit($num);// 按分页取出数据
    $this->_CI->db->order_by('ndate','desc');
	$this->_CI->db->select('ktitle,kid');
    $query = $this->_CI->db->get($bs);
    if($query->num_rows() != 0)
    {
     $data = $query->result_array();

    }

   }else{
   
		$this->_CI->db->where('nshow = 1');
		$this->_CI->db->limit($num);// 按分页取出数据
		$this->_CI->db->order_by('ndate','desc');
		$this->_CI->db->select('ktitle,kid');
		$query = $this->_CI->db->get($bs);
		if($query->num_rows() != 0)
		{
		 $data = $query->result_array();
	
		}

   }

		
					
  return $data;


}











	
///////////////////////////////////////////////函数调用


//论坛最新主题最新回复
// $rs='' 最新主题
// $rs='1' 最新回复

	public function bbs_now($rs='',$where='')
	{
			require ROOT.'browse/home/confhome.php';
			
	
			

			if(empty($rs)){
				$new_bbs_list = $this->bbs();//	;//幻灯片
				foreach($new_bbs_list as $ke => $rs){	
								$bbst[]='<li>';
								$bbst[]='<span>';
								$bbst[]='<a href="/browse/home/bar_type_'.$rs['typeid'].'_1.htm">['.$bar_types[$rs['typeid']].']</a>';
								if ($rs['isbest']) {
									$bbst[]='<a href="/browse/home/bar_best_1.htm">[精华]</a>';
								}
								$bbst[]='<a href="/browse/home/bar_thread_'.$rs['threadid'].'_1.htm"'.(empty($rs['style'])?'':' style="'.$rs['style'].'"').'>'.strip_tags($rs['title']).'</a>';
								$bbst[]='</span>';
								$bbst[]='<i>'.$rs['reply'].'/'.$rs['hit'].'</i>';
								$bbst[]='<em>'.strip_tags($rs['username']).'</em>';
								$bbst[]='<i>'.kc_formatdate($rs['lastdate'],'m-d',1).'</i>';
								$bbst[]='<em>'.strip_tags($rs['lastusername']).'</em>';
								$bbst[]='</li>';
				}		
			}elseif($rs=='1'){
			
	
					$kui=$this->new_row('*',$form='king_reply',$where,'date desc','5');
					$ak=kc_array_tq($kui,'threadid');
					if(empty($ak)){
					return '';
					}
					
					$new_bbs_list=$this->new_row('*',$form='king_thread','threadid in ('.$ak.')','`lastdate desc','5');
					foreach($new_bbs_list as $ke => $rs){	
						
						$k[$rs['threadid']]=$rs;
					
					}
		

				foreach($kui as $ke => $rs){	
				
					$bbst[]='<li><p><strong><a href="#">'.$rs['username'].'</a></strong> : '.kc_short(SpHtml2Text($rs['content']),20,20).'<em> 主题：<a href="/browse/home/bar_thread_'.$k[$rs['threadid']]['threadid'].'_1.htm">'.kc_short(SpHtml2Text($k[$rs['threadid']]['title'],20,10)).'</a> ['.kc_formatdate($rs['date'],'m-d',1).']</em>';
					
							
					$bbst[]='</p></li>';		

				}

			
			}elseif($rs=='3'){
				$new_bbs_list = $this->bbs();//	;//幻灯片
				foreach($new_bbs_list as $ke => $rs){	
								$bbst[]='<li>';
								$bbst[]='<p><strong>';
								$bbst[]='<a href="/browse/home/bar_type_'.$rs['typeid'].'_1.htm">['.$bar_types[$rs['typeid']].']</a></strong> ';
								if ($rs['isbest']) {
									$bbst[]='<a href="/browse/home/bar_best_1.htm">[精华]</a>';
								}
								$bbst[]='<a href="/browse/home/bar_thread_'.$rs['threadid'].'_1.htm"'.(empty($rs['style'])?'':' style="'.$rs['style'].'"').'>'.strip_tags($rs['title']).'</a><br>';
								
	
								$bbst[]=' '.strip_tags($rs['username']).'';
								$bbst[]=' '.kc_formatdate($rs['lastdate'],'m-d',1).'';
								$bbst[]='</p>';
								$bbst[]='</li>';
				}		
			}
				
			echo implode('',$bbst);		
	}
	

//论坛最新商家推荐

//论坛最新文章回复
// $rs='' 最新主题
// $rs='1' 最新回复

	public function bbs_wz($kid='',$typeid='')
	{

		if(empty($kid) && empty($typeid)){  return '';}
		
		$where['kid']=$kid;
		$where['typeid']=$typeid;

		$thread=$this->new_list('king_thread','','',$where,'10','threadid,typeid,hit,reply,isbest,username,userid,lastusername,lastdate,style,title','lastdate desc');
		$where['floor']='1';		
		$reply=$this->new_list('king_reply','','',$where,'10','content,threadid','date desc');

	

		foreach($reply as $ke => $rs){	
		
			$reply[$rs['threadid']]=$rs['content'];	

		}
		foreach($thread as $ke => $rs){	
		
			$thread['list'][$ke]['content']=$reply[$rs['threadid']];	
	
				
			$bbst[]='<li><p><a href="#"><img src="'.kc_userherd($rs['userid'],'small').'" /></a><h3><a href="/browse/home/bar_thread_'.$rs['threadid'].'_1.htm"'.(empty($rs['style'])?'':' style="'.$rs['style'].'"').'>'.strip_tags($rs['title']).'</a></h3>'.kc_short(strip_tags($reply[$rs['threadid']]),'45','2').'</p><span><i>'.formatdate($rs['lastdate'],'m-d',1).'</i><em><a href="#">'.$rs['username'].'</a></em></span>';
			$bbst[]='</li>';		

			

		}
		

		
		if(!empty($thread['list'])){
		$thread['content']= '<ul>'.implode('',$bbst).'</ul>';	
		}else{
		$thread['content']='<p class="p_zt">暂时没有问题</p>';
		}
		return $thread;
		
		
		
		
		
	}









	// 最新文章
	public function shangjai_now()
	{
		$shangj=$this->shangjai_row();

		foreach($shangj as $ke => $rs){	
			if($ke=='0'){
			$r[]='<li class="li_top"><strong><a href="/tag/'.$rs['slug'].'">'.$rs['name'].'</a></strong></li>';}else{
			$r[]='<li><a href="/tag/'.$rs['slug'].'">'.$rs['name'].'</a></li>';
			}
		}
		echo implode('',$r);			
	}
	
	






	
}


?>